package org.springblade.tms.module.userinfo.base;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.tms.global.enums.BindEnum;
import org.springblade.tms.global.enums.CarType;
import org.springblade.tms.global.exception.CommonException;
import org.springblade.tms.module.car.entity.TMCar;
import org.springblade.tms.module.car.service.ICarService;
import org.springblade.tms.module.carriercarrelation.entity.TMCarrierCarRelation;
import org.springblade.tms.module.carriercarrelation.service.ICarrierCarRelationService;
import org.springblade.tms.module.carrierdriverrelation.entity.TMCarrierDriverRelation;
import org.springblade.tms.module.carrierdriverrelation.service.ICarrierDriverRelationService;
import org.springblade.tms.module.carriersupercargorelation.entity.TMCarrierSupercargoRelation;
import org.springblade.tms.module.carriersupercargorelation.service.ICarrierSupercargoRelationService;
import org.springblade.tms.module.carriertankrelation.entity.TMCarrierTankRelation;
import org.springblade.tms.module.carriertankrelation.service.ICarrierTankRelationService;
import org.springblade.tms.module.supercargoinfo.entity.TMSupercargoInfo;
import org.springblade.tms.module.supercargoinfo.service.ISupercargoInfoService;
import org.springblade.tms.module.tank.entity.TMTankTank;
import org.springblade.tms.module.tank.service.ITankTankService;
import org.springblade.tms.module.userinfo.dto.BindDto;
import org.springblade.tms.module.userinfo.dto.NumberBindDto;
import org.springblade.tms.module.userinfo.entity.TMUserInfo;
import org.springblade.tms.module.userinfo.service.IUserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.userinfo.base
 * @className: CheckService
 * @author: yr
 * @description: 绑定服务
 * @date: 2022/3/10 16:43
 */
@Service
@Transactional
public class BindService {
	@Resource
	private ICarrierSupercargoRelationService carrierSupercargoRelationService;
	@Resource
	private ICarrierDriverRelationService carrierDriverRelationService;
	@Resource
	private ICarrierCarRelationService carrierCarRelationService;
	@Resource
	private IUserInfoService userInfoService;
	@Resource
	private ICarService carService;
	@Resource
	private ISupercargoInfoService supercargoInfoService;
	@Resource
	private ITankTankService tankTankService;
	@Resource
	private ICarrierTankRelationService carrierTankRelationService;

	/**
	 * @param bindDto:
	 * @return R
	 * @author yr
	 * @description 司机或车辆绑定解绑
	 * @date 2022/3/10 11:25
	 */
	public R bindOrUnBind(BindDto bindDto) {
		//判断绑定解绑的为司机还是车辆还是押运员
		if (bindDto.getType().equals(BindEnum.CAR.getValue())) {
			//判断绑定还是解绑车辆
			if (bindDto.getBindType().equals(BindEnum.BIND.getValue())) {
				//更新车辆表，用于排序
				List<TMCar> car = carService.lambdaQuery().in(TMCar::getId, bindDto.getBindId()).list();
				carService.updateBatchById(car);
				//绑定车辆
				List<TMCarrierCarRelation> tmCarrierCarRelations = new ArrayList<>();
				bindDto.getBindId().forEach(bindId -> {
					TMCarrierCarRelation tmCarrierCarRelation = new TMCarrierCarRelation();
					tmCarrierCarRelation.setRelationCarrierNo(bindDto.getCarrierId());
					tmCarrierCarRelation.setRelationCarNo(bindId);
					tmCarrierCarRelations.add(tmCarrierCarRelation);
					TMCarrierCarRelation one = carrierCarRelationService.getOne(Condition.getQueryWrapper(tmCarrierCarRelation));
					if (one != null) {
						CommonException.throwCommonException("该车辆已绑定");
					}
				});
				boolean save = carrierCarRelationService.saveOrUpdateBatch(tmCarrierCarRelations);
				return R.status(save);
			} else if (bindDto.getBindType().equals(BindEnum.UNBIND.getValue())) {
				TMCarrierCarRelation carrierCarRelation = carrierCarRelationService.lambdaQuery()
					.eq(TMCarrierCarRelation::getRelationCarrierNo, bindDto.getCarrierId())
					.in(TMCarrierCarRelation::getRelationCarNo, bindDto.getBindId()).one();
				boolean deleteLogic = carrierCarRelationService.deleteLogic(Collections.singletonList(carrierCarRelation.getId()));
				return R.status(deleteLogic);
			}
		} else if (bindDto.getType().equals(BindEnum.DRIVER.getValue())) {
			//判断绑定还是解绑司机
			if (bindDto.getBindType().equals(BindEnum.BIND.getValue())) {
				//更新司机，用于排序
				List<TMUserInfo> driver = userInfoService.lambdaQuery().in(TMUserInfo::getId, bindDto.getBindId()).list();
				userInfoService.updateBatchById(driver);
				//绑定司机
				List<TMCarrierDriverRelation> tmCarrierDriverRelations = new ArrayList<>();
				bindDto.getBindId().forEach(bindId -> {
					TMCarrierDriverRelation tmCarrierDriverRelation = new TMCarrierDriverRelation();
					tmCarrierDriverRelation.setRelationCarrierNo(bindDto.getCarrierId());
					tmCarrierDriverRelation.setRelationDriver(bindId);
					tmCarrierDriverRelations.add(tmCarrierDriverRelation);
					TMCarrierDriverRelation one = carrierDriverRelationService.getOne(Condition.getQueryWrapper(tmCarrierDriverRelation));
					if (one != null) {
						CommonException.throwCommonException("该司机已绑定");
					}
				});
				boolean save = carrierDriverRelationService.saveBatch(tmCarrierDriverRelations);
				return R.status(save);
			} else if (bindDto.getBindType().equals(BindEnum.UNBIND.getValue())) {
				TMCarrierDriverRelation carrierCarRelation = carrierDriverRelationService.lambdaQuery()
					.eq(TMCarrierDriverRelation::getRelationCarrierNo, bindDto.getCarrierId())
					.in(TMCarrierDriverRelation::getRelationDriver, bindDto.getBindId()).one();
				boolean deleteLogic = carrierDriverRelationService.deleteLogic(Collections.singletonList(carrierCarRelation.getId()));
				return R.status(deleteLogic);
			}
		} else if (bindDto.getType().equals(BindEnum.ESCORT.getValue())) {
			//判断绑定还是解绑押运员
			if (bindDto.getBindType().equals(BindEnum.BIND.getValue())) {
				//更新押运员，用于排序
				List<TMSupercargoInfo> supercargoInfo = supercargoInfoService.lambdaQuery().in(TMSupercargoInfo::getId, bindDto.getBindId()).list();
				supercargoInfoService.updateBatchById(supercargoInfo);
				//绑定押运员
				List<TMCarrierSupercargoRelation> tmCarrierSupercargoRelations = new ArrayList<>();
				bindDto.getBindId().forEach(bindId -> {
					TMCarrierSupercargoRelation tmCarrierSupercargoRelation = new TMCarrierSupercargoRelation();
					tmCarrierSupercargoRelation.setRelationCarrierNo(bindDto.getCarrierId());
					tmCarrierSupercargoRelation.setRelationSupercargoNo(bindId);
					tmCarrierSupercargoRelations.add(tmCarrierSupercargoRelation);
					TMCarrierSupercargoRelation one = carrierSupercargoRelationService.getOne(Condition.getQueryWrapper(tmCarrierSupercargoRelation));
					if (one != null) {
						CommonException.throwCommonException("该押运员已绑定");
					}
				});
				boolean save = carrierSupercargoRelationService.saveBatch(tmCarrierSupercargoRelations);
				return R.status(save);
			} else if (bindDto.getBindType().equals(BindEnum.UNBIND.getValue())) {
				TMCarrierSupercargoRelation carrierSupercargoRelation = carrierSupercargoRelationService.lambdaQuery()
					.eq(TMCarrierSupercargoRelation::getRelationCarrierNo, bindDto.getCarrierId())
					.in(TMCarrierSupercargoRelation::getRelationSupercargoNo, bindDto.getBindId()).one();
				boolean deleteLogic = carrierSupercargoRelationService.deleteLogic(Collections.singletonList(carrierSupercargoRelation.getId()));
				return R.status(deleteLogic);
			}
		} else if (bindDto.getType().equals(BindEnum.TANK.getValue())) {
			//判断绑定还是解绑坦克罐
			if (bindDto.getBindType().equals(BindEnum.BIND.getValue())) {
				//更新坦克罐，用于排序
				List<TMTankTank> tmTankTanks = tankTankService.lambdaQuery().in(TMTankTank::getId, bindDto.getBindId()).list();
				tankTankService.updateBatchById(tmTankTanks);
				//绑定坦克罐
				List<TMCarrierTankRelation> tmCarrierTankRelations = new ArrayList<>();
				bindDto.getBindId().forEach(bindId -> {
					TMCarrierTankRelation tmCarrierTankRelation = new TMCarrierTankRelation();
					tmCarrierTankRelation.setRelationCarrierNo(bindDto.getCarrierId());
					tmCarrierTankRelation.setRelationTankNo(bindId);
					tmCarrierTankRelations.add(tmCarrierTankRelation);
					TMCarrierTankRelation one = carrierTankRelationService.getOne(Condition.getQueryWrapper(tmCarrierTankRelation));
					if (one != null) {
						CommonException.throwCommonException("该坦克罐已绑定");
					}
				});
				boolean save = carrierTankRelationService.saveBatch(tmCarrierTankRelations);
				return R.status(save);
			} else if (bindDto.getBindType().equals(BindEnum.UNBIND.getValue())) {
				TMCarrierTankRelation carrierTankRelation = carrierTankRelationService.lambdaQuery()
					.eq(TMCarrierTankRelation::getRelationCarrierNo, bindDto.getCarrierId())
					.in(TMCarrierTankRelation::getRelationTankNo, bindDto.getBindId()).one();
				boolean deleteLogic = carrierTankRelationService.deleteLogic(Collections.singletonList(carrierTankRelation.getId()));
				return R.status(deleteLogic);
			}
		}
		return R.status(false);
	}

	/**
	 * @param numberBindDto:
	 * @return R
	 * @author yr
	 * @description 号码存在时将司机或车辆或押运员绑定给当前承运商
	 * @date 2022/3/10 17:36
	 */
	public R phoneBind(NumberBindDto numberBindDto) {
		TMUserInfo tmUserInfo = getTmUserInfo();
		if (numberBindDto.getType().equals(BindEnum.CAR.getValue())) {
			//更新车辆表，用于排序
			TMCar one = carService.lambdaQuery().eq(TMCar::getCarNo, numberBindDto.getNumber()).one();
			carService.updateById(one);
			//绑定车辆
			TMCarrierCarRelation tmCarrierCarRelation = new TMCarrierCarRelation();
			tmCarrierCarRelation.setRelationCarrierNo(tmUserInfo.getId());
			tmCarrierCarRelation.setRelationCarNo(one.getId());
			boolean save = carrierCarRelationService.save(tmCarrierCarRelation);
			return R.status(save);
		} else if (numberBindDto.getType().equals(BindEnum.BIND_CAR.getValue())) {
			//更新挂车，用于排序
			TMCar one = carService.lambdaQuery().eq(TMCar::getBindCarNo, numberBindDto.getNumber())
				.eq(TMCar::getCarType, CarType.TRAILER.getValue()).one();
			carService.updateById(one);
			//绑定挂车
			TMCarrierCarRelation tmCarrierCarRelation = new TMCarrierCarRelation();
			tmCarrierCarRelation.setRelationCarrierNo(tmUserInfo.getId());
			tmCarrierCarRelation.setRelationCarNo(one.getId());
			boolean save = carrierCarRelationService.save(tmCarrierCarRelation);
			return R.status(save);
		} else if (numberBindDto.getType().equals(BindEnum.ESCORT.getValue())) {
			//更新押运员，用于排序
			TMSupercargoInfo one = supercargoInfoService.lambdaQuery().eq(TMSupercargoInfo::getPhone, numberBindDto.getNumber()).one();
			supercargoInfoService.updateById(one);
			//绑定押运员
			TMCarrierSupercargoRelation tmCarrierSupercargoRelation = new TMCarrierSupercargoRelation();
			tmCarrierSupercargoRelation.setRelationCarrierNo(tmUserInfo.getId());
			tmCarrierSupercargoRelation.setRelationSupercargoNo(one.getId());
			boolean save = carrierSupercargoRelationService.save(tmCarrierSupercargoRelation);
			return R.status(save);
		} else if (numberBindDto.getType().equals(BindEnum.DRIVER.getValue())) {
			//更新司机，用于排序
			TMUserInfo one = userInfoService.lambdaQuery().eq(TMUserInfo::getPhone, numberBindDto.getNumber()).one();
			userInfoService.updateById(one);
			//绑定司机
			TMCarrierDriverRelation tmCarrierDriverRelation = new TMCarrierDriverRelation();
			tmCarrierDriverRelation.setRelationCarrierNo(tmUserInfo.getId());
			tmCarrierDriverRelation.setRelationDriver(one.getId());
			boolean save = carrierDriverRelationService.save(tmCarrierDriverRelation);
			return R.status(save);
		} else if (numberBindDto.getType().equals(BindEnum.TANK.getValue())) {
			//更新坦克罐，用于排序
			TMTankTank one = tankTankService.lambdaQuery().eq(TMTankTank::getTankNumber, numberBindDto.getNumber()).one();
			tankTankService.updateById(one);
			//绑定司机
			TMCarrierTankRelation carrierTankRelation = new TMCarrierTankRelation();
			carrierTankRelation.setRelationCarrierNo(tmUserInfo.getId());
			carrierTankRelation.setRelationTankNo(one.getId());
			boolean save = carrierTankRelationService.save(carrierTankRelation);
			return R.status(save);
		}
		return R.status(false);
	}

	/**
	 * @param :
	 * @return TMUserInfo
	 * @author yr
	 * @description 获取当前用户
	 * @date 2022/3/8 15:17
	 */
	private TMUserInfo getTmUserInfo() {
		Long userId = AuthUtil.getUser().getUserId();
		return userInfoService.lambdaQuery().eq(TMUserInfo::getParentId, userId).one();
	}
}
